package com.zhang.config;

import com.zhang.Boot;
import com.zhang.util.LocalDateChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.annotation.PostConstruct;
import java.nio.charset.Charset;
import java.util.List;

//表明这是一个配置类
@Configuration
//扫描Main这个类所在的包及其子包下的Spring组件
@ComponentScan(basePackageClasses = Boot.class)
//提供MVC的支持
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    /**
     * 设置编码集，只对响应信息有效
     *
     * @param converters
     */
    @Override
    public void extendMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter stringHttpMessageConverter =
                (StringHttpMessageConverter) converters.get(1);
        stringHttpMessageConverter.setDefaultCharset(
                Charset.forName("utf-8"));
    }

    /**
     * 设置静态页面路径
     * 只要请求的/html/下的所有html文件，就会去static/html/下去找对应的html页面
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(
            ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/html/**").
                addResourceLocations("classpath:/static/html/");
    }

    /**
     * 设置日期转换器生效,只要参数是LocalDate类型就自动转换为字符串
     */
    @PostConstruct
    public void addConversionConfig() {
        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter
                .getWebBindingInitializer();
        if (initializer.getConversionService() != null) {
            GenericConversionService converService = (GenericConversionService) initializer.getConversionService();
            converService.addConverter(new LocalDateChange());
        }
    }


//    @Bean
//    public SpringResourceTemplateResolver templateResolver() {
//        SpringResourceTemplateResolver templateResolver =
//                new SpringResourceTemplateResolver();
//        templateResolver.setCharacterEncoding("utf-8");//设置编码集
//        //设置模版引擎页面的路径
//        templateResolver.setPrefix("classpath:/static/");
//        templateResolver.setCacheable(false);
//        return templateResolver;
//    }
//
//    @Bean
//    public SpringTemplateEngine templateEngine() {
//        SpringTemplateEngine templateEngine
//                = new SpringTemplateEngine();
//        templateEngine.setTemplateResolver(templateResolver());
//        return templateEngine;
//    }
//
//
//    @Bean
//    public ThymeleafViewResolver viewResolver() {
//        ThymeleafViewResolver viewResolver =
//                new ThymeleafViewResolver();
//        viewResolver.setCharacterEncoding("utf-8");
//        viewResolver.setTemplateEngine(templateEngine());
//        viewResolver.setOrder(1);
//        //定义html和xhtml后缀的文件被thymeleaf引擎翻译
//        viewResolver.setViewNames(
//                new String[]{"*.html", "*.xhtml"});
//        return viewResolver;
//    }

    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver =
                new CommonsMultipartResolver();
        //设置上传文件大小限制
        multipartResolver.setMaxUploadSize(10240000);
        return multipartResolver;
    }


}